<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

//template library

class template {

	// CI global
    protected $ci;

    //construct
    public function __construct()
    {
        $this->ci =& get_instance();
    }
    
    public function get_date($date)
    {
        $month['1'] = 'มกราคม';
        $month['2'] = 'กุมภาพันธ์';
        $month['3'] = 'มีนาคม';
        $month['4'] = 'เมษายน';
        $month['5'] = 'พฤษภาคม';
        $month['6'] = 'มิถุนายน';
        $month['7'] = 'กรกฎาคม';
        $month['8'] = 'สิงหาคม';
        $month['9'] = 'กันยายน';
        $month['10'] = 'ตุลาคม';
        $month['11'] = 'พฤศจิกายน';
        $month['12'] = 'ธันวาคม';
        //convert to unix timestamp
        $timestamp = strtotime($date);
        $d = date('j', $timestamp);
        $m = $month[date('n', $timestamp)];
        $y = date('Y', $timestamp) + 543;
        $time = date('H:i', $timestamp);
        $text = "$d $m $y เวลา $time น.";
        return $text;
    }
    
    /* 
        // 2 ก.ค.2553 15.00 น.
    */
    public function get_date_template_1 ($date)
    {
        $month['1'] = 'ม.ค.';
        $month['2'] = 'ก.พ.';
        $month['3'] = 'มี.ค.';
        $month['4'] = 'เม.ย.';
        $month['5'] = 'พ.ค.';
        $month['6'] = 'มิ.ย.';
        $month['7'] = 'ก.ค.';
        $month['8'] = 'ส.ค.';
        $month['9'] = 'ก.ย.';
        $month['10'] = 'ต.ค.';
        $month['11'] = 'พ.ค.';
        $month['12'] = 'ธ.ค.';
        
        //convert to unix timestamp
        $timestamp = strtotime($date);
        $d = date('j', $timestamp);
        $m = $month[date('n', $timestamp)];
        $y = date('Y', $timestamp) + 543;
        $time = date('H:i', $timestamp);
        $text = "$d $m $y  $time น.";
        return $text;
    }
    public function get_short_date($date)
    {
        $month['1'] = 'มกราคม';
        $month['2'] = 'กุมภาพันธ์';
        $month['3'] = 'มีนาคม';
        $month['4'] = 'เมษายน';
        $month['5'] = 'พฤษภาคม';
        $month['6'] = 'มิถุนายน';
        $month['7'] = 'กรกฎาคม';
        $month['8'] = 'สิงหาคม';
        $month['9'] = 'กันยายน';
        $month['10'] = 'ตุลาคม';
        $month['11'] = 'พฤศจิกายน';
        $month['12'] = 'ธันวาคม';
        //convert to unix timestamp
        $timestamp = strtotime($date);
        $d = date('j', $timestamp);
        $m = $month[date('n', $timestamp)];
        $y = date('Y', $timestamp) + 543;
        $time = date('H:i', $timestamp);
        $text = "$d $m $y";
        return $text;
    }
    
    public function get_js_date_variable($date)
    {
        $timestamp = strtotime($date);
        $d = date('j', $timestamp);
        $m = date('n', $timestamp) - 1;
        $y = date('Y', $timestamp) ;
        $hour = date('G', $timestamp);
        $min = date('i', $timestamp);
        $sec = date('s', $timestamp);
        
        return "new Date( $y , $m, $d ,$hour ,$min ,$sec)";
    }
    
    public function url_encode($title)
    {
	$string = strtolower(trim($title));
	$string = preg_replace("~[\s]+~","-", $string);
	$string = preg_replace(array("/&amp;/", "/&lt;/", "/&gt;/", "/&quot;/", "/&#xord($0);/", "/&#39;/"), "-", $string);
	$string=preg_replace("/[\-]{2,}/",'-',$string);
        $string = rawurlencode(preg_replace('~[^a-z0-9ก-๙\.\-\_]~iu','', $string));
        //$string = preg_replace('~[^a-z0-9ก-๙\.\-\_]~iu','', $string);
	return trim($string, '-');
    }

    public function photo_thumbs_path($galleries_id, $filename)
    {
        return base_url() . 'uploads/galleries/' . $galleries_id . '/thumbs' . '/' . $filename;
    }
    
    public function photo_path($galleries_id, $filename)
    {
        return base_url() . 'uploads/galleries/' . $galleries_id . '/' . $filename;
    }
    
    public function team_path($team_id)
    {
        return base_url() . 'uploads/teams/' .$team_id. '.gif';
    }
    
    public function prize_path($prize_id)
    {
        return base_url() . 'uploads/prizes/' .$prize_id. '.png';
    }
    
    /**
* Convert timestamp to MySQL's DATE or DATETIME (YYYY-MM-DD hh:mm:ss)
*
* Returns the DATE or DATETIME equivalent of a given timestamp
*
* @author Clemens Kofler <clemens.kofler@chello.at>
* @access    public
* @return    string
*/
    function timestamp_to_mysqldatetime($timestamp = "", $datetime = true)
    {
	if(empty($timestamp) || !is_numeric($timestamp)) $timestamp = time();
	
	return ($datetime) ? date("Y-m-d H:i:s", $timestamp) : date("Y-m-d", $timestamp);
    }
    
    function date_to_mysqldatetime($date = "", $datetime = TRUE)
    {
	return $this->timestamp_to_mysqldatetime($this->date_to_timestamp($date), $datetime);
     }
    
     function date_to_timestamp($datetime = "")
    {
	if (!preg_match("/^(\d{1,2})[.\- \/](\d{1,2})[.\- \/](\d{2}(\d{2})?)( (\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?$/", $datetime, $date))
	    return FALSE;
	    
	$day = $date[1];
	$month = $date[2];
	$year = $date[3];
	$hour = (empty($date[6])) ? 0 : $date[6];
	$min = (empty($date[7])) ? 0 : $date[7];
	$sec = (empty($date[9])) ? 0 : $date[9];
	
	return mktime($hour, $min, $sec, $month, $day, $year);
    }
    
    
    function debug($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }
    
    function parse_signed_request($signed_request, $secret) {
        list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
        
        // decode the data
        $sig = $this->base64_url_decode($encoded_sig);
        $data = json_decode($this->base64_url_decode($payload), true);
        
        if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
            error_log('Unknown algorithm. Expected HMAC-SHA256');
            return null;
        }
        
        // check sig
        $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
        if ($sig !== $expected_sig) {
            error_log('Bad Signed JSON signature!');
            return null;
        }
        
        return $data;
    }

    function base64_url_decode($input) {
        return base64_decode(strtr($input, '-_', '+/'));
    }
    
    function get_profile_picture_url($user_id ,$size = "medium")
    {
        $path = "uploads/users/small/".$user_id.".jpg";
        if (file_exists($path)) {
            return base_url()."uploads/users/".$size."/".$user_id.".jpg";
        } else {
            return base_url()."uploads/users/".$size."/default.png";
        }
    }

}

/* End of file template.php */
/* Location: ./system/application/libraries/template.php */
